<?php
class MovieDetail {        

    public $code;
    public $title;
    public $synopsis;
    public $posterURL;
    public $posterURLLocal;
    public $productionYear;
    public $releaseDate;
    public $directors;
    public $actors;
    public $pressRating;
    public $userRating; 
    public $movieId; 
    public $state; 

    public function save()
    {
        $req = "REPLACE movieDetail SET             
            code           = "  . $this->code  . " ,
            movieId        = "  . $this->movieId  . " ,
            title          = '" . $this->title  . "' ,
            synopsis       = '" . str_replace('\n',"<br>",str_replace(";","\;",str_replace("'","\'",$this->synopsis)))  . "' ,
            posterURL      = '" . $this->posterURL . "' ,
            posterURLLocal = '" . $this->posterURLLocal . "' ,
            productionYear = "  . $this->productionYear .",
            releaseDate    = '" . $this->releaseDate . "' ,
            directors      = '" . $this->directors . "' ,
            actors         = '" . $this->actors . "' ,
            pressRating    ="   . $this->pressRating .",
            state          ="   . $this->state .",
            userRating     ="   . $this->userRating ."
            ";
        $result = mysql_query($req);
        
        return $result;

    }
    public function downloadPoster(){        
        
        $from = $this->posterURL;
        $to = "posters/" . $this->code . "_poster.gif";
        $handler = fopen($from, "r"); 
        $contents = ''; 
        if($handler) { 
            while(!feof($handler))
                $contents .= fread($handler, 8192); 
            fclose($handler); 
        }   
        $handlew = fopen($to, "w"); 
        fwrite($handlew, $contents); 
        fclose($handlew);

        $this->posterURLLocal = $to;
        $this->save();    
    }

    public function disableMovieDetail()
    {
        $req = "UPDATE movieDetail SET state = "   . 0 ." where code =" . $this->code ." ";
                
        $result = mysql_query($req);
        
        return $result;

    }

    public function loadMovieDetail()
    {
        $query    = "SELECT * FROM movieDetail where code=" . $this->code . " and state = 1 ORDER BY code";
                
        $result   = mysql_query($query);
        if(mysql_num_rows($result)!=0){
            if($row = mysql_fetch_assoc($result)){   
                $this->movieId        = $row["movieId"];
                $this->title          = $row["title"];
                $this->synopsis       = $row["synopsis"];
                $this->posterURL      = $row["posterURL"];
                $this->posterURLLocal = $row["posterURLLocal"];
                $this->productionYear = $row["productionYear"];
                $this->releaseDate    = $row["releaseDate"];
                $this->directors      = $row["directors"];
                $this->actors         = $row["actors"];
                $this->pressRating    = $row["pressRating"];
                $this->userRating     = $row["userRating"];
                $this->state          = $row["state"];
            }
        }
    }
    public function majPoster()
    {
        if($this->posterURLLocal == null){

            $from = $this->posterURL;
            $to = "posters/" . $this->code . "_poster.gif";
            
            $handler = fopen($from, "r"); 
            $contents = ''; 
            if($handler) { 
                while(!feof($handler))
                    $contents .= fread($handler, 8192); 
                fclose($handler); 
            }   
            $handlew = fopen($to, "w"); 
            fwrite($handlew, $contents); 
            fclose($handlew);

            $this->posterURLLocal = $to;
            $this->save();
            
        }
    }
}


             